package cn.example.demo.modules.house.controller;

import cn.example.demo.common.model.response.HttpDataResponseResult;
import cn.example.demo.common.model.service.ServiceResult;
import cn.example.demo.common.model.response.HttpResponseResult;
import cn.example.demo.modules.house.dto.HouseTypeDto;
import cn.example.demo.modules.house.entity.HouseType;
import cn.example.demo.modules.house.service.IHouseTypeService;
import cn.example.demo.common.retrieval.PageBean;
import cn.example.demo.common.secure.annotation.AuthEnable;
import cn.example.demo.common.validation.constraint.ParamRegex;
import cn.example.demo.common.validation.groups.Insert;
import cn.example.demo.common.validation.groups.Update;
import cn.example.demo.common.validation.groups.Delete;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import cn.hutool.core.util.StrUtil;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 【房源类型】控制器
 * </p>
 */
@Validated
@RestController
@RequestMapping("api/HouseType")
@Api(tags = {"【房源类型】操作接口"})
public class HouseTypeController {
    @Resource
    private IHouseTypeService houseTypeService;

    @AuthEnable
    @PostMapping("add")
    @ApiOperation(value = "新增【房源类型】")
    public HttpResponseResult addHouseType(@ApiParam(value = "房源类型请求参数体") @RequestBody @Validated(Insert.class) HouseTypeDto dto) {
        // 保存HouseType
        ServiceResult result = houseTypeService.insertHouseType(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }
        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @PutMapping("modify")
    @ApiOperation(value = "修改【房源类型】")
    public HttpResponseResult modifyHouseType(@ApiParam(value = "房源类型请求参数体") @RequestBody @Validated(Update.class) HouseTypeDto dto) {
        // 保存HouseType
        ServiceResult result = houseTypeService.updateHouseType(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @GetMapping(value = "retrieval")
    @ApiOperation(value = "查询【房源类型】")
    public HttpResponseResult getHouseTypeList(@ApiParam(value = "房源类型请求参数体") HouseTypeDto dto) {
        PageBean result = houseTypeService.queryHouseType(dto);

        if (result.getItems() == null || result.getItems().isEmpty()) {
            return HttpDataResponseResult.isNotFound("未查询到【房源类型】记录！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @GetMapping(value = "find/{id}")
    @ApiOperation(value = "根据 ID 查询单个【房源类型】")
    public HttpResponseResult getOneHouseType(@ApiParam(value = "房源类型ID") @PathVariable @NotBlank(message = "id不能为空！") String id) {
        HouseType result = houseTypeService.findHouseTypeById(id);

        if (result == null) {
            return HttpDataResponseResult.isNotFound(StrUtil.format("未查询到【房源类型】记录！ID:{}", id), null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @DeleteMapping("remove")
    @ApiOperation(value = "删除【房源类型】")
    public HttpResponseResult removeHouseType(@ApiParam(value = "房源类型请求参数体") @RequestBody @Validated(Delete.class) HouseTypeDto dto) {
        ServiceResult result = houseTypeService.deleteHouseType(dto);

        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), result.getData());
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @GetMapping(value = "dictList")
    @ApiOperation(value = "以字典表格式查询【房源类型】")
    public HttpResponseResult getHouseTypeAsDict(@ApiParam(value = "房源类型请求参数体") HouseTypeDto dto) {
        List result = houseTypeService.queryHouseTypeAsDict(dto);
        if (ObjectUtil.isEmpty(result)) {
            return HttpDataResponseResult.isNotFound("【房源类型】列表：没有数据！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }
}
